package controller

import (
	"context"
	"distributed-cache/bufferpoll"
	"distributed-cache/percolator"
	"encoding/json"
	"strings"
)

func Put(ctx context.Context, virtualPool *bufferpoll.VirtualPool, key string, value string) {
	hashNum := percolator.StringToHash(key)
	go bufferpoll.SetOperation(ctx, hashNum&1023, strings.Join([]string{key, value}, ":"), virtualPool)
}

func Get(ctx context.Context, virtualPool *bufferpoll.VirtualPool, key string) string {
	hashNum := percolator.StringToHash(key)
	value := bufferpoll.GetResult(ctx, hashNum&1023, key, virtualPool)
	body, err := json.Marshal(value)
	if err != nil {
		return "{\\\"flag\\\":true,\\\"value\\\":\\\"v2\\\"}"
	}
	return string(body)
}
